package com.itheima.reggie.config;

import org.springframework.cache.annotation.CachingConfigurationSelector;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * 通过配置类来指定key序列化器为string，我们在测试类注入的redisTemplate对象是我们自定义的
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisTemplate<Object,Object> redisTemplate (RedisConnectionFactory redisConnectionFactory){
        //自定义一个RedisTemplate对象，当然我们自己不创建的话，SpringBoot也会帮我们创建
        RedisTemplate<Object,Object> redisTemplate=new RedisTemplate<>();
        //默认的key序列化器是jdk，我们改为string，这样就不会是字节输出了
        redisTemplate.setKeySerializer(new StringRedisSerializer());//这是设置简单类型数据
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());//设置hash类型数据的key序列化器为string
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }

}
